#! /usr/bin/env python
#coding=utf-8
import time, sys
#from charts.models import *
import datetime
from datetime import date

from sqlalchemy import *
from uliweb.orm import *
from uliweb.utils.pyini import Ini

def get_db_connection():
    connect = Ini('../apps/settings.ini').ORM.CONNECTION
    db = get_connection(connect, debug=False)
    #db = get_connection('mysql://ctasks:123456@localhost/ctasks?charset=utf8')
    return db


def get_month():
    #获取报表当前月第一天
    today = sys.argv[1]
    def getMonth(): 
        #return str(datetime.date.today())[5:7]
        return str(today)[5:7] 
    def strtodatetime(datestr,format):       
        return datetime.date(*time.strptime(datestr, format)[:3])  
    def getYear():   
        #return str(datetime.date.today())[0:4]
        return str(today)[0:4] 
    
    format="%Y-%m-%d" 
    
    
    #获取当前月份 是一个字符串    
    year =  getYear()
    month = getMonth()
    oneday = datetime.timedelta(days=1)
    
    #转换当前月份的显示格式
    month_view = str(month) + '月'
    
    #判断本月末是否是季度末
    #quater = long(month)
    #if(quater < 3) and (quater <> 3) and (quater <> 6) and (quater <> 9) and (quater <> 12):
    #    sys.exit()
    
    #next_month是季度末日期，即为本月末日期
    next_month = long(month)+1
    #判断当前月是否为12月，若为12月则月份赋值1月，年度增加1年
    next_year = str(year)
    if(next_month == 13):
        next_month = 1
        next_year = str(long(year)+1)
    next_month = str(next_month)
    
    #计算季度初始日期，即为本季度第一个月1日
    #first_month = long(month) - 2
    first_month = long(month)
    first_month = str(first_month)
    first_day = str(year) + '-' + first_month + '-01'
    last_day = next_year + '-' + next_month + '-01'
    
    month_first_day = strtodatetime(first_day,format)
    month_last_day = strtodatetime(last_day,format)-oneday
    #month_first_day = strtodatetime('2010-12-01',format)
    #month_last_day = strtodatetime('2011-01-01',format)-oneday
    
    return  month_first_day,month_last_day,month_view

def get_quarter():
    today = sys.argv[1]
    def getMonth(): 
        #return str(datetime.date.today())[5:7]
        return str(today)[5:7] 
    def strtodatetime(datestr,format):       
        return datetime.date(*time.strptime(datestr, format)[:3])   
    def getYear():   
        #return str(datetime.date.today())[0:4]
        return str(today)[0:4] 
    
    
    format="%Y-%m-%d" 
    
    
    #获取当前月份 是一个字符串    
    year =  getYear()
    month = getMonth()
    oneday = datetime.timedelta(days=1)
    
    #判断本月末是否是季度末
    quarter = long(month)
    if(quarter <> 3) and (quarter <> 6) and (quarter <> 9) and (quarter <> 12):
        sys.exit()
    
    #获取当前季度信息
    if(quarter == 3):
        quarter_view = '一季度'
    elif(quarter == 6):
        quarter_view = '二季度'
    elif(quarter == 9):
        quarter_view = '三季度'
    elif(quarter == 12):
        quarter_view = '四季度'
    
    #next_month是季度末日期，即为本月末日期
    next_month = long(month)+1
    #判断当前月是否为12月，若为12月则月份赋值1月，年度增加1年
    next_year = str(year)
    if(next_month == 13):
        next_month = 1
        next_year = str(long(year)+1)
    next_month = str(next_month)
    
    #计算季度初始日期，即为本季度第一个月1日
    first_month = long(month) - 2
    #first_month = long(month)
    first_month = str(first_month)
    first_day = str(year) + '-' + first_month + '-01'
    last_day = next_year + '-' + next_month + '-01'
    
    month_first_day = strtodatetime(first_day,format)
    month_last_day = strtodatetime(last_day,format)-oneday
    
    #month_first_day = strtodatetime('2010-10-01',format)
    #month_last_day = strtodatetime('2011-01-01',format)-oneday
    
    return  month_first_day,month_last_day,quarter_view,quarter
